package io.yunho.yunho.interview.two.lock;

import java.util.concurrent.CountDownLatch;

/**
 * @author: yunho
 * @date: 2021/7/23 15:57
 * @description:
 */
public class CountDownLanchTest {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        CountDownLanchDemo countDownLanchDemo = new CountDownLanchDemo(countDownLatch);
        for(int i=0;i<10;i++){
            new Thread(()->{
                countDownLanchDemo.runTest();
            },String.valueOf(i)).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("总耗时：***** \t"+ (end-start));
    }
}

class CountDownLanchDemo{
    private CountDownLatch countDownLatch;

    public CountDownLanchDemo(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }

    public void runTest(){
        synchronized (this) {
            try {
                System.out.println(Thread.currentThread().getName() + "********runing  CountDownLanchDemo:" + this.countDownLatch.getCount());
            } finally {//确保执行
                this.countDownLatch.countDown();
            }
        }
//        try {
//            System.out.println(Thread.currentThread().getName() + "********runing start CountDownLanchDemo:" + this.countDownLatch.getCount());
//        } finally {//确保执行
//            this.countDownLatch.countDown();
//            System.out.println(Thread.currentThread().getName() + "********runing end CountDownLanchDemo:" + this.countDownLatch.getCount());
//        }
    }

}